#include "PlainLineRenderer.h"
#include "SDL_opengl.h"

namespace k2graphics
{

PlainLineRenderer::PlainLineRenderer()
{
	setColor(0, 1.0f, 0.0f, 1.0f);
	setColor(1, 1.0f, 0.0f, 0.0f);
	setColor(2, 0.0f, 1.0f, 0.0f);
	setColor(3, 0.0f, 0.0f, 1.0f);
}

void PlainLineRenderer::renderObject(RenderData* data, GraphicsInternals* gi)
{
	// the line should have at least four float values (x1, y1) (x2, y2) and
	// one byte value (color)

	if(data->numBytes < 1 || data->numFloats < 4)
		return;

	int theColor = data->byteData[0];
	int numSegments = data->numFloats / 4;

	// set openGL color
	glColor3f(presetColors[theColor][0], presetColors[theColor][1], presetColors[theColor][2]);

	// draw line
	glBegin(GL_LINES);

	for(int i = 0; i < numSegments * 4; i += 4)
	{
		glVertex2f(data->floatData[i + 0], data->floatData[i + 1]);
		glVertex2f(data->floatData[i + 2], data->floatData[i + 3]);
	}

	glEnd();
}

void PlainLineRenderer::setColor(int color, float r, float g, float b)
{
	presetColors[color][0] = r;
	presetColors[color][1] = g;
	presetColors[color][2] = b;
}

}